#!BPY

"""
Name: 'Java class (.java)'
Blender: 244
Group: 'Export'
Tooltip: 'Exports header file for use with the OpenGL ES'
"""

import Blender
from Blender import *
import bpy
import bpy
import os

def save(filepath):    
    out = file(filepath, 'w')
    sce = bpy.data.scenes.active
    ob = sce.objects.active
    mesh = Mesh.New()        
    mesh.getFromObject(ob.name)

    editmode = Window.EditMode()
    if editmode: Window.EditMode(0)
    has_quads = False
    for f in mesh.faces:
        if len(f) == 4:
            has_quads = True
            break
    
    if has_quads:
        oldmode = Mesh.Mode()
        Mesh.Mode(Mesh.SelectModes['FACE'])
        
        mesh.sel = True
        tempob = sce.objects.new(mesh)
        mesh.quadToTriangle(0) # more=0 shortest length
        oldmode = Mesh.Mode(oldmode)
        sce.objects.unlink(tempob)
        
        Mesh.Mode(oldmode)
    
    objectname = ob.getData(True)
    basename = objectname.capitalize()

    one = 65536 
    obj = one / 4

    out.write('package cz.maestro.sokodroid;\n\npublic class Vertex {\n Vertex(){}\npublic static int [] getData() {\n')
    out.write('int %sVertexData[] = {\n' % basename)
    for face in mesh.faces:
        for (vert, uvert) in zip(face.verts, face.uv):
            out.write('\t%d, %d, %d,   ' % (vert.co.x * obj , vert.co.y * obj, vert.co.z * obj) )
            out.write('%d, %d, %d,   ' % (vert.no.x * one, vert.no.y * one, vert.no.z * one))
            out.write('%d, %d,' % ( (uvert.x) * one, (1-uvert.y) * one ) )
            out.write('\n')
    out.write('};\n\n')
    
    if editmode: Window.EditMode(1)
    out.write('return  %sVertexData;\n}' % basename)
    out.write('public static int size = %i;\n}' % (len(mesh.faces) * 3) )

    out.close()

filename = os.path.splitext(Blender.Get('filename'))[0]
Blender.Window.FileSelector(save, "Export", '%s.h' % filename)
